from app import create_app, db
from app.models import MonitorPoint, MonitorData
from datetime import datetime, timedelta
import random

def create_history_data():
    app = create_app()
    with app.app_context():
        # 清除现有的测试数据
        MonitorData.query.delete()
        MonitorPoint.query.delete()
        
        # 创建测试监控点
        test_points = [
            {
                'name': '1号养殖池',
                'location': 'A区-东侧',
                'description': '草鱼养殖池'
            },
            {
                'name': '2号养殖池',
                'location': 'A区-西侧',
                'description': '鲫鱼养殖池'
            },
            {
                'name': '3号养殖池',
                'location': 'B区-北侧',
                'description': '鲤鱼养殖池'
            }
        ]
        
        created_points = []
        for point_data in test_points:
            point = MonitorPoint(**point_data)
            db.session.add(point)
            created_points.append(point)
        
        db.session.commit()
        
        # 为每个监控点生成30天的测试数据
        now = datetime.utcnow()
        for point in created_points:
            # 生成过去30天的数据，每天24条数据（每小时一条）
            for days_ago in range(30, -1, -1):
                base_date = now - timedelta(days=days_ago)
                for hour in range(24):
                    timestamp = base_date.replace(hour=hour, minute=0, second=0, microsecond=0)
                    
                    # 生成合理范围内的随机数据，添加一些波动性
                    hour_factor = abs(12 - hour) / 12  # 使数据随时间变化
                    day_factor = days_ago / 30  # 使数据随天数变化
                    
                    # 基础值加上随机波动
                    dissolved_oxygen = 6.0 + random.uniform(-1.0, 1.0) + hour_factor
                    ph_value = 7.5 + random.uniform(-0.5, 0.5)
                    temperature = 24.0 + random.uniform(-2.0, 2.0) + 3 * hour_factor
                    ammonia_nitrogen = 0.3 + random.uniform(-0.1, 0.1) + 0.1 * day_factor
                    turbidity = 20.0 + random.uniform(-5.0, 5.0) + 5 * day_factor
                    
                    # 确保数值在合理范围内
                    data = MonitorData(
                        point_id=point.id,
                        dissolved_oxygen=round(max(4.0, min(8.0, dissolved_oxygen)), 2),
                        ph_value=round(max(6.5, min(8.5, ph_value)), 2),
                        temperature=round(max(20.0, min(28.0, temperature)), 1),
                        ammonia_nitrogen=round(max(0.1, min(0.5, ammonia_nitrogen)), 2),
                        turbidity=round(max(10.0, min(30.0, turbidity)), 2),
                        timestamp=timestamp
                    )
                    db.session.add(data)
        
        db.session.commit()
        print('历史数据创建成功！')
        
        # 打印统计信息
        points_count = MonitorPoint.query.count()
        data_count = MonitorData.query.count()
        print(f'已创建 {points_count} 个监控点')
        print(f'已创建 {data_count} 条监控数据')
        print('数据范围：')
        print('- 溶解氧: 4.0-8.0 mg/L')
        print('- pH值: 6.5-8.5')
        print('- 温度: 20.0-28.0 ℃')
        print('- 氨氮: 0.1-0.5 mg/L')
        print('- 浊度: 10.0-30.0 NTU')

if __name__ == '__main__':
    create_history_data() 